import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.*;
import java.awt.image.BufferedImage;

public class Display extends Applet implements MouseListener,MouseMotionListener, Runnable, KeyListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final int LVLWIDTH = 1000;
	private static final int LVLHEIGHT = 860;
	private Thread runner;
	private boolean firstPass = true;
	private BufferedImage buff;
	private Graphics2D buffGraph;
	private int X;
	private int Y;
	private boolean landPress;
	private boolean objectPress;
	private boolean enemyPress;
	private boolean esc;
	private boolean enter;
	private boolean PRESSED;
	private boolean delete;
	private boolean shift;
	
	LevelEditor levelEditor;
	
	public void init()
	{
		levelEditor = new LevelEditor(this);
		this.setSize(LVLWIDTH, LVLHEIGHT);
		runner = new Thread(this);
		runner.start();
		this.addKeyListener(this);
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
	}
	
	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		PRESSED = true;
		X = arg0.getX();
		Y = arg0.getY();
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		PRESSED = false;
	}

	@Override
	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub
		X = arg0.getX();
		Y = arg0.getY();
	}

	@Override
	public void mouseMoved(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	//Paint
	public void paint(Graphics g)
	{
		update(g);
	}
	
	public void update(Graphics g)
	{
		if(firstPass){
			buff = (BufferedImage)this.createImage(LVLWIDTH, LVLHEIGHT);
			buffGraph = buff.createGraphics();
			
			firstPass = false;
		}
		
		levelEditor.draw(buffGraph, this);
		g.drawImage(buff, 0, 0, this);
	}
	
	//Run
	public void run() {
		
		while(true)
			{
		// TODO Auto-generated method stub
		try {
			Thread.sleep(15);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	

		
		levelEditor.act(X, Y, landPress, objectPress, enemyPress, esc, enter, delete, shift, PRESSED);
		
		if(enter)//Had to set enter to false due to a strange error what made it true for three cycles regardless.
		{
			levelEditor.compileLevel();
			enter = false;
		}
		
		this.repaint();
		
			}
		
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		if(e.getKeyCode() == KeyEvent.VK_1)
			landPress = true;

		
		if(e.getKeyCode() == KeyEvent.VK_2)
			objectPress = true;

		
		if(e.getKeyCode() == KeyEvent.VK_3)
			enemyPress = true;

		
		if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
			esc = true;

		
		if(e.getKeyCode() == KeyEvent.VK_ENTER)
			enter = true;

		if(e.getKeyCode() == KeyEvent.VK_DELETE)
			delete = true;
		
		if(e.getKeyCode() == KeyEvent.VK_SHIFT)
			shift = true;
			
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		if(e.getKeyCode() == KeyEvent.VK_1)
			landPress = false;
		
		if(e.getKeyCode() == KeyEvent.VK_2)
			objectPress = false;
		
		if(e.getKeyCode() == KeyEvent.VK_3)
			enemyPress = false;
		
		if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
			esc = false;
		
		if(e.getKeyCode() == KeyEvent.VK_ENTER)
			enter = false;
		
		if(e.getKeyCode() == KeyEvent.VK_DELETE)
			delete = false;
		
		if(e.getKeyCode() == KeyEvent.VK_SHIFT)
			shift = false;
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	
}
